#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++) 
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-', ch = getchar(); 
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return f ? -x : x;
}

const int N = 2e5 + 10;

int c[N], n;
veg ans;
bool v[N];

inline void SWAP(const int &x, const int &y) { ans.eb(x, y); c[x] = -c[x]; c[y] = -c[y]; swap(c[x], c[y]); }

inline void cyc(int x, int y) {
	SWAP(x, y);
	int cur = x;
	while(c[-c[cur]] > 0) SWAP(cur, -c[cur]);
	cur = -c[cur];
	while(c[-c[cur]] > 0) SWAP(cur, -c[cur]);
	SWAP(cur, -c[cur]);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in; rep(i, 1, n) c[i] = in;
	int lst = 0;
	rep(i, 1, n) {
		if(c[i] == i || v[i]) continue;
		int x = i;
		while(!v[x]) v[x] = true, x = c[x];
		if(!lst) lst = i;
		else cyc(i, lst), lst = 0;
	}
	if(lst) {
		bool fl = false;
		rep(i, 1, n) if(c[i] == i) { cyc(i, lst), fl = true; break; }
		if(!fl) { int a = c[lst], b = c[a]; SWAP(lst, a); cyc(a, b); }
	}
	printf("%lu\n", ans.size());
	for(auto d : ans) printf("%d %d\n", d.fi, d.se);
	return 0;
}
